
/* GCSx
** IMGDIALOG.CPP
**
** Image selection dialog (from animgroup/tileset)
** Not a full dialog- used as a base for others
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

ImageDialog::ImageDialog() : Dialog(blankString) { start_func
    libTree = NULL;
    imgChoose = NULL;
    world = NULL;
    libId = 0;
}

ImageDialog::~ImageDialog() { start_func
    if (libTree) {
        WidgetScroll* ws = dynamic_cast<WidgetScroll*>(libTree->getParent());
        if (ws) ws->newClient();
        delete libTree;
    }
}

void ImageDialog::addLibTree(int id) { start_func
    libTree = new TreeView(blankString, NULL, 0, NULL, 1);
    libTree->addTo(this, 0, 5, id);
    libId = id;
}

void ImageDialog::addImgChoose(int id) { start_func
    imgChoose = new ImageChooser(NULL);
    int w = -1;
    int h = -1;
    if (libId) {
        WidgetScroll* ws = dynamic_cast<WidgetScroll*>(findWidget(libId));
        if (ws) {
            w = ws->getClientWidth();
            h = ws->getClientHeight();
        }
    }
    imgChoose->addTo(this, w, h, id);
}

void ImageDialog::selectImage(AnimGroup* animGroup, TileSet* tileSet, int animTileId) { start_func
    TreeView* found = NULL;
    if (animGroup) {
        found = libTree->findRecursive(animGroup->getId(), treeViewWrapAnimGroup);
    }
    else if (tileSet) {
        found = libTree->findRecursive(tileSet->getId(), treeViewWrapTileSet);
    }
    if (found) {
        found->selectMe();
        imgChoose->cursorState(animTileId - 1);
    }
}

void ImageDialog::fillLibTree() { start_func
    libTree->removeAll();

    if (world) {
        libTree->insert(new TreeView("(no image)", this, 0, treeViewWrapAnimGroup), (animId == 0) && (tileId == 0));
        TreeView* item = NULL;

        World::AnimGroupIndex::const_iterator endAL = world->endAnimGroup();
        for (World::AnimGroupIndex::const_iterator pos = world->beginAnimGroup(); pos != endAL; ++pos) {
            AnimGroupEdit* pAnimGroup = dynamic_cast<AnimGroupEdit*>((*pos).second);
            libTree->insert(item = new TreeView(pAnimGroup->getName(), this, pAnimGroup->getId(), treeViewWrapAnimGroup), pAnimGroup->getId() == animId);
            item->setIcon(7);
        }
    
        World::TileSetIndex::const_iterator endTS = world->endTileSet();
        for (World::TileSetIndex::const_iterator pos = world->beginTileSet(); pos != endTS; ++pos) {
            TileSetEdit* pTileSet = dynamic_cast<TileSetEdit*>((*pos).second);
            libTree->insert(item = new TreeView(pTileSet->getName(), this, pTileSet->getId(), treeViewWrapTileSet), pTileSet->getId() == tileId);
            item->setIcon(pTileSet->getIsFont() ? 4 : (pTileSet->getCollisionCount() ? 9 : 5));
        }
    }
}
    
int ImageDialog::treeViewWrapTileSet(void* ptr, int code, int command, int check) { start_func
    return ((ImageDialog*)ptr)->treeViewTileSet(code, command, check);
}

int ImageDialog::treeViewTileSet(int code, int command, int check) { start_func
    if (check) {
        return Window::COMMAND_HIDE;
    }
    
    if (command == LV_MOVE) {
        TileSetEdit* tileset = dynamic_cast<TileSetEdit*>(world->findTileSet(code));
        if (tileset) {
            animId = 0;
            tileId = tileset->getId();
            imgChoose->setTileset(tileset);
            imgChoose->cursorState(subId - 1);
        }
        return 1;
    }
    
    return 0;
}
    
int ImageDialog::treeViewWrapAnimGroup(void* ptr, int code, int command, int check) { start_func
    return ((ImageDialog*)ptr)->treeViewAnimGroup(code, command, check);
}

int ImageDialog::treeViewAnimGroup(int code, int command, int check) { start_func
    if (check) {
        return Window::COMMAND_HIDE;
    }
    
    if (command == LV_MOVE) {
        if (code) {
            AnimGroupEdit* animgroup = dynamic_cast<AnimGroupEdit*>(world->findAnimGroup(code));
            if (animgroup) {
                tileId = 0;
                animId = animgroup->getId();
                imgChoose->setTileset(NULL);
                imgChoose->cursorState(subId - 1);
            }
        }
        else {
            tileId = 0;
            animId = 0;
            imgChoose->setTileset(NULL);
            imgChoose->cursorState(subId - 1);
        }
        return 1;
    }
    
    return 0;
}

void ImageDialog::runComplete(AnimGroup** animGroup, TileSet** tileSet, int* animTileId, int justChecking) { start_func
    if ((animId) || (tileId))
        subId = imgChoose->cursorState() + 1;
    else
        subId = 0;
    
    AnimGroup* aLib = NULL;
    TileSet* tSet = NULL;

    if ((animId) && (subId > 0)) {
        aLib = world->findAnimGroup(animId);
        if (!aLib->getCount()) {
            subId = 0;
            aLib = NULL;
        }
        else assert(subId <= aLib->getCount());
    }
    else if ((tileId) && (subId > 0)) {
        tSet = world->findTileSet(tileId);
        if (!tSet->getCount()) {
            subId = 0;
            tSet = NULL;
        }
        else assert(subId <= tSet->getCount());
    }
    else {
        subId = 0;
    }

    if (animGroup) *animGroup = aLib;
    if (tileSet) *tileSet = tSet;
    if (animTileId) *animTileId = subId;
    
    if (!justChecking) {
        imgChoose->setTileset(NULL);
        world = NULL;
    }
}

void ImageDialog::runPrep(WorldEdit* nWorld, AnimGroup* animGroup, TileSet* tileSet, int animTileId) { start_func
    world = nWorld;

    animId = tileId = subId = 0;
    if (animGroup) {
        animId = animGroup->getId();
        subId = animTileId;
    }
    else if (tileSet) {
        tileId = tileSet->getId();
        subId = animTileId;
    }

    fillLibTree();
}
